﻿#include "stdafx.h"
#include "DefectMap.h"
#include "DefectMapSetting.h"
#include "time.h"
#include "qsettings.h"
#include "qdebug.h"
#include "SignalCenter.h"

DefectMap::DefectMap(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	//<!暂时屏蔽不需要的界面功能
	ui.pushButton_Restore->setVisible(false);
	ui.pushButton_Setting->setVisible(false);
	ui.doubleSpinBox_lowLimit->setVisible(false);
	ui.doubleSpinBox_highLimit->setVisible(false);
	ui.label->setVisible(false);
	ui.label_2->setVisible(false);
	ui.pushButton_Jump->setVisible(false);
	
	init_customPlot();
	init_OthersControl();
	init_DefectMapInfo();

	m_pMenu = new QMenu(this);
	m_pMenu->setParent(this);

	QList<QAction*> actionList;

	//<! 右键菜单
	QAction* pAction = new QAction(tr("适应"), this);
	connect(pAction, &QAction::triggered, this, &DefectMap::on_pushButton_Restore_clicked);
	actionList.append(pAction);

	pAction = new QAction(tr("设置"), this);
	connect(pAction, &QAction::triggered, this, &DefectMap::on_pushButton_Setting_clicked);
	actionList.append(pAction);

	m_pMenu->addActions(actionList);

	connect(&SignalCenter::GetInstance(), SIGNAL(addDefectMapData(int, double, double)), 
		this, SLOT(addData(int, double, double)));
}

DefectMap::~DefectMap()
{
	saveCfg();
}

void DefectMap::init_customPlot()
{
	ui.customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect /*| QCP::iSelectAxes*/| QCP::iSelectLegend);
	ui.customPlot->setBackground(QColor(0, 0, 0, 0));

	//设置图像作为背景
	//QPixmap map("OPT_logo.jpg");
	//ui.customPlot->setBackground(map, true);

	ui.customPlot->xAxis->setRange(rangeLower_X, rangeUpper_X);
	ui.customPlot->yAxis->setRange(rangeLower_Y, rangeUpper_Y);
	ui.customPlot->xAxis->setLabelColor(QColor(255,255,255));
	ui.customPlot->yAxis->setLabelColor(QColor(255, 255, 255));
	ui.customPlot->xAxis->setTickLabelColor(QColor(255, 255, 255));
	ui.customPlot->yAxis->setTickLabelColor(QColor(255, 255, 255));
	ui.customPlot->xAxis->setSubTickPen(Qt::NoPen);
	ui.customPlot->yAxis->setSubTickPen(Qt::NoPen);
	ui.customPlot->xAxis->setBasePen(QColor(206, 206, 206));
	ui.customPlot->yAxis->setBasePen(QColor(206, 206, 206));

	//ui.customPlot->xAxis2->setVisible(true);
	//ui.customPlot->xAxis2->setTickLabels(false);     //设置y轴刻度值不显示
	//ui.customPlot->xAxis2->setBasePen(Qt::NoPen);    //设置y2轴的绘制画笔
	//ui.customPlot->xAxis2->setTickPen(Qt::NoPen);    //设置y2轴的主刻度线绘制画笔
	//ui.customPlot->xAxis2->setSubTickPen(Qt::NoPen); //设置y2轴的子刻度线绘制画笔
	//ui.customPlot->axisRect()->axis(QCPAxis::atTop, 0)->setPadding(100);

	
	ui.customPlot->xAxis->setLabel(tr("区域"));
	ui.customPlot->yAxis->setLabel(tr("位置"));


	ui.customPlot->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignLeft | Qt::AlignTop);
	ui.customPlot->legend->setVisible(true);
	QFont legendFont = font();
	legendFont.setPointSize(10);
	ui.customPlot->legend->setTextColor(QColor(69, 83, 100));
	ui.customPlot->legend->setFont(legendFont);
	ui.customPlot->legend->setSelectedFont(legendFont);
	ui.customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
	//ui.customPlot->legend->simplify();
	ui.customPlot->legend->setBorderPen(QColor(255,255,255));
	ui.customPlot->legend->setBrush(QBrush(QColor(255, 255, 255, 0)));
	ui.customPlot->legend->setSelectedBorderPen(QPen(QColor(255, 215, 0)));
	ui.customPlot->legend->setSelectedTextColor(QColor(255, 215, 0));
	
	ui.customPlot->legend->setColumnSpacing(2); 
	//ui.customPlot->legend->setWrap(4); //<! 换行/列
	//ui.customPlot->legend->setFillOrder(QCPLayoutGrid::foColumnsFirst);//<!横向布局
	//ui.customPlot->legend->setSizeConstraintRect(QCPLayoutElement::scrOuterRect);
	/*int count = ui.customPlot->plotLayout()->rowCount();
	ui.customPlot->plotLayout()->addElement(count, 0, ui.customPlot->legend);*///<! 重新布局legend的位置

	connect(ui.customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(customplot_mousePress(QMouseEvent*)));
	connect(ui.customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(customplot_mouseWheel(QWheelEvent*)));
	connect(ui.customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(customplot_mouseMove(QMouseEvent*)));
	connect(ui.customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(customplot_mouseRelease(QMouseEvent*)));
	connect(ui.customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged()));

	connect(ui.customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*, int, QMouseEvent*)), this,
		SLOT(customplot_plottableClick(QCPAbstractPlottable*, int, QMouseEvent*)));

	tracer = new QCPItemTracer(ui.customPlot);		//生成游标
	tracer->setPen(QPen(Qt::red));					//圆圈轮廓颜色
	tracer->setBrush(QBrush(Qt::red));				//圆圈圈内颜色
	tracer->setStyle(QCPItemTracer::tsCircle);		//圆圈
	tracer->setSize(5);								//设置大小

	tracerLabel = new QCPItemText(ui.customPlot); //生成游标说明
	tracerLabel->setLayer("overlay");//设置图层为overlay，因为需要频繁刷新
	tracerLabel->setColor(QColor(255, 215, 0));
	tracerLabel->setPositionAlignment(Qt::AlignLeft | Qt::AlignTop);//左上
	tracerLabel->position->setParentAnchor(tracer->position);//将游标说明锚固在tracer位置处，实现自动跟随

	//<! 选中才显示
	tracer->setVisible(false);
	tracerLabel->setVisible(false);

}

void DefectMap::init_OthersControl()
{
	ui.doubleSpinBox_lowLimit->setValue(rangeLower_Y);
	ui.doubleSpinBox_highLimit->setValue(rangeUpper_Y);
}

void DefectMap::init_DefectMapInfo()
{
	/*for (int i = 0; i < 4; i++)
	{
		DefectMapInfo info;
		info.symbolName = (QStringLiteral("P")) + QString::number(i);
		info.symbolType = i + 3;
		this->appandBlemishMapInfo(info);
	}*/
	QVector<DefectMapInfo> infoVec;
	QVector<QCPGraph*> graphics;
	loadCfg(infoVec);
	for (size_t i = 0; i < infoVec.size(); i++)
	{
		auto graph = appendBlemishMapInfo(infoVec[i]);
		graphics.push_back(graph);
	}
	//<! 清空自动排布
	for (size_t i = 0; i < ui.customPlot->legend->elementCount(); i++)
	{
		ui.customPlot->legend->removeAt(i);
	}
	
	QCPTextElement* title = new QCPTextElement(ui.customPlot, tr("机台编号"), QFont("sans", 8, QFont::Bold));
	title->setTextColor(QColor(255, 255, 255));
	ui.customPlot->legend->addElement(0, 0, title);

	int index = 0;
	for each (auto& var in graphics)
	{
		bool bRet = ui.customPlot->legend->addElement(1, index, new QCPPlottableLegendItem(ui.customPlot->legend, var));
		index++;
	}

}

void DefectMap::reflesh_checkBoxInfo()
{
	if (m_blemishMapInfo.count() >= 4)
	{
		ui.checkBox_blemishType_1->setText(m_blemishMapInfo[0].symbolName);
		ui.checkBox_blemishType_2->setText(m_blemishMapInfo[1].symbolName);
		ui.checkBox_blemishType_3->setText(m_blemishMapInfo[2].symbolName);
		ui.checkBox_blemishType_4->setText(m_blemishMapInfo[3].symbolName);
	}
}

void DefectMap::customplot_mousePress(QMouseEvent*  event)
{
	QCPRange rangeY = ui.customPlot->yAxis->range();
	mouseClickLower_Y = rangeY.lower;
	mouseClickUpper_Y = rangeY.upper;

	QCPRange rangeX = ui.customPlot->xAxis->range();
	mouseClickLower_X = rangeX.lower;
	mouseClickUpper_X = rangeX.upper;

	if (ui.customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
		ui.customPlot->axisRect()->setRangeDrag(ui.customPlot->xAxis->orientation());
	else if (ui.customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
		ui.customPlot->axisRect()->setRangeDrag(ui.customPlot->yAxis->orientation());
	else
		ui.customPlot->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical);

	//<! 右键菜单
	if (event->button() == Qt::RightButton)
	{
		QCursor cur = this->cursor();
		m_pMenu->exec(cur.pos());
	}

}

void DefectMap::customplot_mouseWheel(QWheelEvent* event)
{

	if (ui.customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis))
		ui.customPlot->axisRect()->setRangeZoom(ui.customPlot->xAxis->orientation());
	else if (ui.customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis))
		ui.customPlot->axisRect()->setRangeZoom(ui.customPlot->yAxis->orientation());
	else
		ui.customPlot->axisRect()->setRangeZoom(Qt::Horizontal | Qt::Vertical);



	QCPRange rangeY = ui.customPlot->yAxis->range();

	if (rangeY.lower < rangeLower_Y)
	{
		ui.customPlot->yAxis->setRangeLower(rangeLower_Y);
	}

	/*if (rangeY.upper > rangeUpper_Y)
	{
		ui.customPlot->yAxis->setRangeUpper(rangeUpper_Y);
	}*/
	ui.customPlot->replot();
	QCPRange rangeX = ui.customPlot->xAxis->range();
	if (rangeX.lower < rangeLower_X)
	{
		ui.customPlot->xAxis->setRangeLower(rangeLower_X);
	}

	/*if (rangeX.upper > rangeUpper_X)
	{
		ui.customPlot->xAxis->setRangeUpper(rangeUpper_X);
	}*/
	rangeY = ui.customPlot->yAxis->range();

	ui.doubleSpinBox_highLimit->setValue(rangeY.upper);
	ui.doubleSpinBox_lowLimit->setValue(rangeY.lower);
	ui.customPlot->replot();
}

void DefectMap::customplot_mouseMove(QMouseEvent* e)
{
	double x = ui.customPlot->xAxis->pixelToCoord(e->pos().x());
	double y = ui.customPlot->yAxis->pixelToCoord(e->pos().y());
	QString displayLabel = "x:" + QString::number(x) + " y:" + QString::number(y);
	ui.lineEdit->setText(displayLabel);


}

void DefectMap::customplot_mouseRelease(QMouseEvent* e)
{
	ui.customPlot->axisRect()->setRangeDrag(ui.customPlot->yAxis->orientation());
	QCPRange rangeY = ui.customPlot->yAxis->range();
	double minRangeY = rangeLower_Y - 300;//<! 最小y坐标
	if (rangeY.lower < minRangeY)
	{
		ui.customPlot->yAxis->setRangeLower(minRangeY);
		ui.customPlot->yAxis->setRangeUpper(mouseClickUpper_X);
	}

	//if (rangeY.upper > rangeUpper_Y)
	//{
	//	ui.customPlot->yAxis->setRangeUpper(rangeUpper_Y);
	//	ui.customPlot->yAxis->setRangeLower(mouseClickLower_Y);
	//}

	ui.customPlot->replot();

	double minRangeX = rangeLower_X - 300;//<! 最小y坐标
	QCPRange rangeX = ui.customPlot->xAxis->range();
	if (rangeX.lower < (rangeLower_X-100)) //<！ 最小x坐标
	{
		ui.customPlot->xAxis->setRangeLower(minRangeX);
	}

	//if (rangeX.upper > rangeUpper_X)
	//{
	//	ui.customPlot->xAxis->setRangeUpper(rangeUpper_X);
	//}

	rangeY = ui.customPlot->yAxis->range();
	ui.doubleSpinBox_highLimit->setValue(rangeY.upper);
	ui.doubleSpinBox_lowLimit->setValue(rangeY.lower);
	ui.customPlot->replot();
}

void DefectMap::selectionChanged()
{
	// make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object:
	if (ui.customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui.customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
		ui.customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui.customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
	{
		ui.customPlot->xAxis2->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
		ui.customPlot->xAxis->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
	}
	// make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object:
	if (ui.customPlot->yAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui.customPlot->yAxis->selectedParts().testFlag(QCPAxis::spTickLabels) ||
		ui.customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui.customPlot->yAxis2->selectedParts().testFlag(QCPAxis::spTickLabels))
	{
		ui.customPlot->yAxis2->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
		ui.customPlot->yAxis->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
	}

	bool itemSelected = false;
	// synchronize selection of graphs with selection of corresponding legend items:
	for (int i = 0; i < ui.customPlot->graphCount(); ++i)
	{
		QCPGraph* graph = ui.customPlot->graph(i);
		QCPPlottableLegendItem* item = ui.customPlot->legend->itemWithPlottable(graph);
		if (/*item->selected() ||*/ graph->selected())
		{
			item->setSelected(true);
			graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
			itemSelected = true;		
		}
		else
		{
			item->setSelected(false);	
		}
		
	}
	if (itemSelected)
	{
		tracer->setVisible(true);
		tracerLabel->setVisible(true);
	}
	else
	{
		tracer->setVisible(false);
		tracerLabel->setVisible(false);
	}
	ui.customPlot->replot();
}

void DefectMap::customplot_plottableClick(QCPAbstractPlottable* plottable, int dataIndex, QMouseEvent* event)
{
	int graphId = 0;
	QString labelName = plottable->name();

	for each (DefectMapInfo var in m_blemishMapInfo)
	{
		if (var.symbolName == labelName)
		{
			break;
		}
		graphId++;
	}

	const QCPGraphData* ghd = ui.customPlot->graph(graphId)->data()->at(dataIndex);

	//获得鼠标位置处对应的横坐标数据x
	/*double x = ui.customPlot->xAxis->pixelToCoord(event->pos().x());
	double y = ui.customPlot->yAxis->pixelToCoord(event->pos().y());*/
	double xValue, yValue;

	xValue = ghd->key;//xValue就是游标的横坐标
	yValue = ghd->value;//yValue就是游标的纵坐标，这里直接根据产生数据的函数获得

	QString label = "" + labelName + "\r\nx:" + QString::number(xValue, 'f', 2) +
		"\r\ny:" + QString::number(yValue, 'f', 2);

	tracer->position->setCoords(xValue, yValue);//设置游标位置
	tracerLabel->setTextAlignment(Qt::AlignLeft);
	tracerLabel->setText(label);//设置游标说明内容'
	ui.customPlot->replot();//绘制器一定要重绘，否则看不到游标位置更新情况

	userClickPlottable(graphId, xValue, yValue);
}

void DefectMap::set_CustomPlot_style()
{

}

void DefectMap::set_CustomPlotLabel_style()
{

}

void DefectMap::on_pushButton_Jump_clicked()
{
	ui.customPlot->yAxis->setRange(ui.doubleSpinBox_lowLimit->value(), ui.doubleSpinBox_highLimit->value());
	ui.customPlot->replot();
}

void DefectMap::on_pushButton_Restore_clicked()
{
	ui.customPlot->yAxis->setRange(rangeLower_Y, rangeUpper_Y);
	ui.customPlot->xAxis->setRange(rangeLower_X, rangeUpper_X);
	ui.customPlot->replot();
}

void DefectMap::on_pushButton_Setting_clicked()
{
	DefectMapSettingDlg* dialog = new DefectMapSettingDlg();
	dialog->setAttribute(Qt::WA_DeleteOnClose);
	dialog->initBlemishInfo(&m_blemishMapInfo);
	int result = dialog->exec();
	if (result == 0)
	{
		int i = 0;
		for each (DefectMapInfo var in m_blemishMapInfo)
		{
			int displayType = var.symbolType;
			QString displayName = var.symbolName;
			ui.customPlot->graph(i)->setLineStyle(QCPGraph::LineStyle::lsNone);
			ui.customPlot->graph(i)->setName(displayName);
			ui.customPlot->graph(i)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(displayType), var.color, 10));
			QPen graphPen = ui.customPlot->graph()->pen();
			graphPen.setWidthF(5);
			ui.customPlot->graph(i)->setPen(graphPen);
			i++;
		}
		ui.customPlot->replot();
		saveCfg();
	}

}

void DefectMap::on_checkBox_blemishType_1_stateChanged(int status)
{
	int qcp_infoNumber = 0;
	if (status == 0)
	{
		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle::ssNone);
		ui.customPlot->replot();
	}
	else if (status == 2)
	{
		if (m_blemishMapInfo.count() < 1)
		{
			return;
		}
		int DisplayType = m_blemishMapInfo[qcp_infoNumber].symbolType;
		
		QString name = m_blemishMapInfo[qcp_infoNumber].symbolName;

		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(DisplayType), m_blemishMapInfo[qcp_infoNumber].color, 10));

	}
	ui.customPlot->replot();
}

void DefectMap::on_checkBox_blemishType_2_stateChanged(int status)
{
	int qcp_infoNumber = 1;
	if (status == 0)
	{
		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle::ssNone);
		ui.customPlot->replot();
	}
	else if (status == 2)
	{
		if (m_blemishMapInfo.count() < 2)
		{
			return;
		}
		int DisplayType = m_blemishMapInfo[qcp_infoNumber].symbolType;
	
		QString name = m_blemishMapInfo[qcp_infoNumber].symbolName;

		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(DisplayType), m_blemishMapInfo[qcp_infoNumber].color, 10));
	}
	ui.customPlot->replot();
}

void DefectMap::on_checkBox_blemishType_3_stateChanged(int status)
{
	int qcp_infoNumber = 2;
	if (status == 0)
	{
		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle::ssNone);
		ui.customPlot->replot();
	}
	else if (status == 2)
	{
		if (m_blemishMapInfo.count() < 3)
		{
			return;
		}
		int DisplayType = m_blemishMapInfo[qcp_infoNumber].symbolType;
		QString name = m_blemishMapInfo[qcp_infoNumber].symbolName;

		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(DisplayType), m_blemishMapInfo[qcp_infoNumber].color, 10));
	}
	ui.customPlot->replot();
}

void DefectMap::on_checkBox_blemishType_4_stateChanged(int status)
{
	int qcp_infoNumber = 3;
	if (status == 0)
	{
		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle::ssNone);
		ui.customPlot->replot();
	}
	else if (status == 2)
	{
		if (m_blemishMapInfo.count() < 4)
		{
			return;
		}
		int DisplayType = m_blemishMapInfo[qcp_infoNumber].symbolType;

		QString name = m_blemishMapInfo[qcp_infoNumber].symbolName;

		ui.customPlot->graph(qcp_infoNumber)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(DisplayType), m_blemishMapInfo[qcp_infoNumber].color, 10));
	}
	ui.customPlot->replot();
}

void DefectMap::setCustomplotRangeY(double lower, double higher)
{
	rangeUpper_Y = higher;
	rangeLower_Y = lower;
	ui.doubleSpinBox_highLimit->setMinimum(higher);
	ui.doubleSpinBox_highLimit->setMaximum(higher);
}

void DefectMap::getCustomplotRangeY(double& lower, double& higher)
{
	lower = rangeLower_Y;
	higher = rangeUpper_Y;
}

void DefectMap::setCustomplotRangeX(double lower, double higher)
{
	rangeUpper_X = higher;
	rangeLower_X = lower;
	ui.customPlot->xAxis->setRange(higher, lower);
}

void DefectMap::getCustomplotRangeX(double& lower, double& higher)
{
	lower = rangeLower_X;
	higher = rangeUpper_X;
}

void DefectMap::setBlackGrounpImage(QString imageFilePath)
{
	backgroundImg.load(mapBackgroundImgFilePath);
	ui.customPlot->setBackground(backgroundImg, true);
}

QCPGraph* DefectMap::appendBlemishMapInfo(DefectMapInfo MapInfo)
{
	
	m_blemishMapInfo.append(MapInfo);
	int index = m_blemishMapInfo.count() - 1;
	QCPGraph* graph = ui.customPlot->addGraph();
	ui.customPlot->graph(index)->setName(MapInfo.symbolName);
	ui.customPlot->graph(index)->setLineStyle(QCPGraph::LineStyle::lsNone);
	ui.customPlot->graph(index)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(MapInfo.symbolType), MapInfo.color, 10));
	QPen graphPen = ui.customPlot->graph()->pen();
	graphPen.setWidthF(5);
	ui.customPlot->graph(index)->setPen(graphPen);
	

	//auto item = ui.customPlot->legend->item(index + 1);
	//ui.customPlot->legend->removeItem(item);

	//bool bRet = ui.customPlot->legend->addElement(1, index, new QCPPlottableLegendItem(ui.customPlot->legend, graph));
	//ui.customPlot->legend->updateLayout();
	ui.customPlot->replot();

	
	reflesh_checkBoxInfo();

	if (index == 0)
		ui.checkBox_blemishType_1->setCheckState(Qt::CheckState::Checked);
	else if (index == 1)
		ui.checkBox_blemishType_2->setCheckState(Qt::CheckState::Checked);
	else if (index == 2)
		ui.checkBox_blemishType_3->setCheckState(Qt::CheckState::Checked);
	else if (index == 3)
		ui.checkBox_blemishType_4->setCheckState(Qt::CheckState::Checked);
	return graph;
}

void DefectMap::addData(int grapchIndex, double x, double y)
{
	ui.customPlot->graph(grapchIndex)->addData(x, y);
	ui.customPlot->replot();
}

void DefectMap::clearData(int grapchIndex)
{
	ui.customPlot->graph(grapchIndex)->data().clear();
}

void DefectMap::clearAllData()
{
	int count = ui.customPlot->graphCount();
	for (int i = 0; i < count; i++)
	{
		ui.customPlot->graph(i)->data().clear();
	}

}

void DefectMap::refresh()
{
	ui.customPlot->replot();
}

void DefectMap::loadCfg(QVector<DefectMapInfo>& infoVec)
{
	m_blemishMapInfo.clear();
	DefectMapInfo info;
	QSettings settings("./Cfg.ini",QSettings::IniFormat);
	info.symbolName = settings.value("DefectMap/ScatterName1", "P0").toString();
	info.symbolType = settings.value("DefectMap/SymbolShape1", 1).toInt();
	info.color = settings.value("DefectMap/SymbolColor1", QColor(255,255,255)).value<QColor>();
	infoVec.push_back(info);
	info.symbolName = settings.value("DefectMap/SymbolName2", "P1").toString();
	info.symbolType = settings.value("DefectMap/SymbolShape2", 2).toInt();
	info.color = settings.value("DefectMap/SymbolColor2", QColor(255, 255, 255)).value<QColor>();
	infoVec.push_back(info);
	info.symbolName = settings.value("DefectMap/SymbolName3", "P2").toString();
	info.symbolType = settings.value("DefectMap/SymbolShape3", 3).toInt();
	info.color = settings.value("DefectMap/SymbolColor3", QColor(255, 255, 255)).value<QColor>();
	infoVec.push_back(info);
	info.symbolName = settings.value("DefectMap/SymbolName4", "P3").toString();
	info.symbolType = settings.value("DefectMap/SymbolShape4", 4).toInt();
	info.color = settings.value("DefectMap/SymbolColor4", QColor(255, 255, 255)).value<QColor>();
	infoVec.push_back(info);
}

void DefectMap::saveCfg()
{
	DefectMapInfo info;
	QSettings settings("./Cfg.ini", QSettings::IniFormat);
	info = m_blemishMapInfo[0];
	settings.setValue("DefectMap/SymbolName1", info.symbolName);
	settings.setValue("DefectMap/SymbolShape1", info.symbolType);
	settings.setValue("DefectMap/SymbolColor1", info.color);
	info = m_blemishMapInfo[1];
	settings.setValue("DefectMap/SymbolName2", info.symbolName);
	settings.setValue("DefectMap/SymbolShape2", info.symbolType);
	settings.setValue("DefectMap/SymbolColor2", info.color);
	info = m_blemishMapInfo[2];
	settings.setValue("DefectMap/SymbolName3", info.symbolName);
	settings.setValue("DefectMap/SymbolShape3", info.symbolType);
	settings.setValue("DefectMap/SymbolColor3", info.color);
	info = m_blemishMapInfo[3];
	settings.setValue("DefectMap/SymbolName4", info.symbolName);
	settings.setValue("DefectMap/SymbolShape4", info.symbolType);
	settings.setValue("DefectMap/SymbolColor4", info.color); 
	
}


